﻿//-----------------------------------------------------------------------------
// <copyright file="MainWindowViewModel.cs" company="William E. Kempf">
//     Copyright (c) William E. Kempf.
// </copyright>
//-----------------------------------------------------------------------------

namespace Simple
{
    using System.Windows;
    using System.Windows.Input;
    using Onyx;
    using Onyx.ComponentModel;
    using Onyx.Windows;

    /// <summary>
    /// The <em>View Model</em> for the <see cref="MainWindow"/> <em>View Element</em>.
    /// </summary>
    public class MainWindowViewModel : ViewModel
    {
        /// <summary>
        /// The TestMessageDisplay routed command.
        /// </summary>
        private static readonly RoutedUICommand cmdTestMessageDisplay =
            new System.Windows.Input.RoutedUICommand(
                "Display Message",
                "TestMessageDisplay",
                typeof(MainWindowViewModel));

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
        /// </summary>
        /// <param name="view">The view to associate with this instance.</param>
        public MainWindowViewModel(View view)
            : base(view)
        {
            this.CommandBindings.Add(new CommandBinding(TestMessageDisplay, this.TestMessageDisplayExecuted));
        }

        /// <summary>
        /// Gets the TestMessageDisplay command.
        /// </summary>
        /// <value>The TestMessageDisplay command.</value>
        public static RoutedUICommand TestMessageDisplay
        {
            get { return cmdTestMessageDisplay; }
        }

        /// <summary>
        /// Handles the execution of the <see cref="MainWindowPresenter.TestMessageDisplay"/> command.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void TestMessageDisplayExecuted(object sender, RoutedEventArgs args)
        {
            var displayMessage = this.View.GetService<IDisplayMessage>();
            if (displayMessage != null)
            {
                var testMessage = this.View.GetService<ITestMessage>();
                if (testMessage != null)
                {
                    displayMessage.ShowMessage(testMessage.TestMessage);
                }
            }
        }
    }
}
